/*
 * Copyright (c) 2011, C-Talks and/or its owners. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact C-Talks owners, Matias Dumrauf or Facundo Viale
 * or visit https://sites.google.com/site/utnfrbactalks/ if you need additional
 * information or have any questions.
 */

/*
 *      @FILE: config_loader.c
 *      @AUTOR: Facundo Viale
 *      @VERSION:       1.0     (10/09/2008)
 *                                      - First Release
 */

#include <string.h>
#include <stdlib.h>
#include "configLoader.h"
#include "fileio.h"
#include "scanner.h"
#include <stdint.h>


//------------------PFS--------------------------

tipoConfigPFS leerArchConfigPFS(){
	tipoConfigPFS configPFS;
	char *archivo = config_loader_open("/home/alejandro/configPFS.txt");
	configPFS.ip=config_loader_getString(archivo, "ip: ");
	configPFS.puerto=config_loader_getInt(archivo, "puerto: ");
	configPFS.maxCon=config_loader_getInt(archivo, "maxCon: ");
	configPFS.tamCache=config_loader_getInt(archivo, "tamCache: ");
	configPFS.logArchPath=config_loader_getString(archivo, "logArchPath: ");
	free(archivo);
	return configPFS;
}

//------------------------------------------------

//------------------PRAID-------------------------
tipoConfigPRAID leerArchConfigPRAID(char *archivoConfig){
	tipoConfigPRAID configPRAID;
	char *archivo = config_loader_open(archivoConfig);
	configPRAID.ip = config_loader_getString(archivo, "ip: ");
	configPRAID.puerto = config_loader_getInt(archivo, "puerto: ");
	configPRAID.consolaActivada = config_loader_getInt(archivo, "consolaActivada: ");
	free(archivo);
	return configPRAID;
}
//------------------------------------------------

//------------------PPD-------------------------
tipoConfigPPD leerArchConfigPPD(){
	tipoConfigPPD configPPD;
	char *archivo = config_loader_open("/home/utn_so/configPPD.txt");
	configPPD.ip = config_loader_getString(archivo, "ip: ");
	configPPD.puerto = config_loader_getInt(archivo, "puerto: ");
	configPPD.flag = config_loader_getInt(archivo, "flag: ");
	configPPD.modo = config_loader_getString(archivo, "modo: ");
	configPPD.algoritmo = config_loader_getString(archivo, "algoritmo: ");
	configPPD.id_disco = config_loader_getString(archivo, "id_disco: ");
	//configPPD.CHS = config_loader_getInt(archivo, "CHS: ") ??
	configPPD.tiempo_lectura = config_loader_getInt(archivo, "tiempo_lectura: ");
	configPPD.tiempo_escritura = config_loader_getInt(archivo, "tiempo_escritura: ");
	configPPD.rpm = config_loader_getInt(archivo, "rpm: ");
	configPPD.salto_pistas = config_loader_getInt(archivo, "salto_pistas: ");
	free(archivo);
	return configPPD;
}
//------------------------------------------------

char *config_loader_open(char* path){
        return fileio_openfile(path);
}

char *config_loader_getString(char *configBuff, char *key){
        char *retValue = NULL, *line;
        int keysize = strlen(key);

        int closure(char *line){ return scanner_starWith(line, key) == 1; }

        line = scanner_findLine(configBuff, &closure);

        if( line != NULL ){
                int tmpIndex = keysize;
                for(; line[tmpIndex]!='\n' && line[tmpIndex]!='\0'; tmpIndex++);

                retValue = malloc( tmpIndex + 1 );
                memcpy(retValue, &line[keysize], tmpIndex);
                retValue[tmpIndex]='\0';

                free(line);
        }

        return retValue;
}

int config_loader_getInt(char *configBuff, char *key){
        char *data = config_loader_getString(configBuff, key);

        if( data != NULL){
                int x = atoi(data);
                free(data);
                return x;
        }
        return 0;
}

double config_loader_getDouble(char *configBuff, char *key){
        char *data = config_loader_getString(configBuff, key);

        if( data != NULL){
                double x = atof(data);
                free(data);
                return x;
        }
        return 0;
}
